package com.crazypos.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crazypos.constant.CustomerStatusConstatnt;
import com.crazypos.dao.CustomerDao;
import com.crazypos.exceptions.MyException;
import com.crazypos.pojo.CustomerEntity;
import com.crazypos.pojo.bo.CreateCustomerBO;
import com.crazypos.service.ICustomerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CustomerService extends ServiceImpl<CustomerDao, CustomerEntity> implements ICustomerService {

    @Autowired
    private CustomerDao customerDao;

    private CustomerEntity checkPhoneOrEmailExists(String organizationId, String phone, String email) {
        CustomerEntity entity = null;
        if (!StringUtils.isBlank(phone) || !StringUtils.isBlank(email)) {
            QueryWrapper<CustomerEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("organization_id", organizationId);
            if (!StringUtils.isBlank(phone)) {
                queryWrapper.eq("phone", phone);
            }
            if (!StringUtils.isBlank(email)) {
                queryWrapper.eq("email", phone);
            }
            entity = customerDao.selectOne(queryWrapper);
        }
        return entity;
    }

    public IPage<CustomerEntity> getCusomtersList(Integer startPage, Integer pageSize, String keyword, String organizationId) {
        if (startPage == null || startPage <= 0) {
            startPage = 1;
        }

        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }

        QueryWrapper<CustomerEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organization_id", organizationId);
        if (!StringUtils.isBlank(keyword)) {
            queryWrapper.like("name", keyword.trim())
                    .or().like("email", keyword.trim())
                    .or().like("phone", keyword.trim());
        }

        Page<CustomerEntity> page = new Page<>(startPage, pageSize);

        return customerDao.selectPage(page, queryWrapper);
    }

    public void createCustomer(CreateCustomerBO bo, String userId, String organizationId) {
        CustomerEntity existsEntity = checkPhoneOrEmailExists(organizationId, bo.getPhone(), bo.getEmail());
        if (existsEntity != null) {
            throw new MyException(500, "手机号或邮箱已经被使用");
        }

        CustomerEntity customerEntity = new CustomerEntity();
        BeanUtil.copyProperties(bo, customerEntity);
        customerEntity.setOrganizationId(organizationId);
        customerEntity.setCreatedBy(userId);
        customerEntity.setStatus(CustomerStatusConstatnt.NORMAL);

        customerDao.insert(customerEntity);
    }

}
